package com.sys.utils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class TextReader {

    public static List<String> read(String fileName,String charset) throws IOException {
        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        List<String> retList = new ArrayList<>();
        try {
            fis = new FileInputStream(fileName);
            // 字节流转字符流，可以指定编码格式
            isr = new InputStreamReader(fis,charset);
            br = new BufferedReader(isr);

            // 读取一行
            String line = br.readLine();
            while(line != null){ // 读取的行不为null，说明后面还有内容，就可以继续读
                retList.add(line);// 将每一行字符串放入list
                line = br.readLine();
            }

        } catch (FileNotFoundException e) {
            throw e;
        } catch (IOException e) {
            throw e;
        } finally {

            if(br !=null){
                try {
                    br.close();
                } catch (IOException e) {
                    throw e;
                }
            }
            if(isr!=null){ // 先打开的后关闭
                try {
                    isr.close();
                } catch (IOException e) {
                    throw e;
                }
            }
            if(fis!=null){ // 先打开的后关闭
                try {
                    fis.close();
                } catch (IOException e) {
                    throw e;
                }
            }

        }
        return retList;
    }
}
